﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleCompiler
{
    public class StatementCallFunction : Statement
    {
        public List<Variable> inVariables = new List<Variable>(); // from left to right
        public string functionName = "";
        public StatementFunction statementFunction = new StatementFunction();
        public override Variable Execute(RuntimeVariableScope scope)
        {
            statementFunction = scope.GetFunctionStatementByName(functionName);
            List<Variable> inConstVariables = new List<Variable>();
            for (int paramIndex = 0; paramIndex < statementFunction.inParamNames.Count; paramIndex++)
            {
                Variable variable = inVariables[paramIndex];
                inConstVariables.Add(variable.ToStatementConstVariable().Execute(scope));
            }
            scope.PushScope(true);
            for (int paramIndex = 0; paramIndex < statementFunction.inParamNames.Count; paramIndex++)
            {
                Variable variable = inVariables[paramIndex];
                Variable constVariable = inConstVariables[paramIndex];
                string name = statementFunction.inParamNames[paramIndex];
                constVariable.name = name;
                scope.InitVariable(name, constVariable);
            }
            Variable functionVariable = statementFunction.Execute(scope);
            scope.PopScope();
            return functionVariable;
        }
    }
}
